package com.atguigu.flink08;

import com.atguigu.beans.WaterSensor;
import com.atguigu.func.WaterSensorMapFunction;
import org.apache.flink.api.common.state.ListState;
import org.apache.flink.api.common.state.ListStateDescriptor;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.util.Collector;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Felix
 * @date 2024/2/27
 * 该案例演示列表状态
 * 需求：针对每种传感器输出最高的3个水位值
 */
public class Flink04_State_ListState {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        SingleOutputStreamOperator<WaterSensor> wsDS = env
                .socketTextStream("hadoop102", 8888)
                .map(new WaterSensorMapFunction());

        wsDS
                .keyBy(WaterSensor::getId)
                .process(
                        new KeyedProcessFunction<String, WaterSensor, String>() {
                            //声明列表状态
                            ListState<Integer> vcListState;

                            @Override
                            public void open(Configuration parameters) throws Exception {
                                //列表状态初始化
                                ListStateDescriptor<Integer> listStateDescriptor
                                        = new ListStateDescriptor<Integer>("vcListState", Integer.class);
                                vcListState = getRuntimeContext().getListState(listStateDescriptor);
                            }

                            @Override
                            public void processElement(WaterSensor ws, KeyedProcessFunction<String, WaterSensor, String>.Context ctx, Collector<String> out) throws Exception {
                                //将当前传感器对应的水位值放到状态中
                                vcListState.add(ws.vc);

                                //从状态获取数据
                                Iterable<Integer> vcListIt = vcListState.get();

                                //将数据放到List集合中
                                List<Integer> vcList = new ArrayList<>();
                                for (Integer vc : vcListIt) {
                                    vcList.add(vc);
                                }
                                //排序
                                vcList.sort((o1, o2) -> o2 - o1);

                                if(vcList.size() > 3){
                                    vcList.remove(3);
                                }

                                out.collect("传感器id为" + ws.getId() + ",最大的3个水位值=" + vcList.toString());

                                //vcListState.clear();

                            }
                        }
                ).print();

        env.execute();
    }
}
